package com.dhsc.service.impl.jyjk.zxyc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dhsc.domain.jkzx.zxjh.FzszVo;
import com.dhsc.domain.jkzx.zxjh.JhbsVo;
import com.dhsc.domain.jkzx.zxjh.JkxmZdVo;
import com.dhsc.domain.jkzx.zxjh.JkyclsVo;
import com.dhsc.domain.jkzx.zxjh.YcjkyzVo;
import com.dhsc.domain.jkzx.zxjh.ZRuleVo;
import com.dhsc.domain.jkzx.zxjh.ZhgVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.exception.MyException;
import com.dhsc.service.jyjk.zxyc.IJklxszService;
import com.dhsc.util.CommonUtil;
import com.dhsc.view.dao.jyjk.zxyc.IJklxszDao;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;


@Service("jklxszService")
@Slf4j
public class JklxszSerivceImpl implements IJklxszService {

    @Autowired
    private IJklxszDao jklxszDao;

    @Override
    public ResponseObject selectDictionaryInfo(JkxmZdVo j) {
        ResponseObject obj = new ResponseObject();
        try{
            List<JkxmZdVo> list = jklxszDao.selectDictionaryInfo(j);
            obj.setData(list);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("查询监控项目字典信息出错", e);
            throw new MyException("查询监控项目字典信息出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject updateZRuleActivate(JhbsVo j) {
        ResponseObject obj = new ResponseObject();
        try{
            jklxszDao.updateZRuleActivate(j);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("激活启用或禁用账户实名制异常规则出错", e);
            throw new MyException("激活启用或禁用账户实名制异常规则出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject delZRuleInfo(ZRuleVo z) {
        ResponseObject obj = new ResponseObject();
        try{
            jklxszDao.delZRuleInfo(z);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("删除异常监控规则出错", e);
            throw new MyException("删除异常监控规则出错",e);
        }
        return obj;
    }

    @Transactional("viewTransactionManager")
    @Override
    public ResponseObject againdataurl(ZRuleVo z){
        ResponseObject obj = new ResponseObject();
            //正回购监控重算
            JkyclsVo jkyclsVo = new JkyclsVo();
            jkyclsVo.setKssj(CommonUtil.getDateTime());
            jkyclsVo.setXcmc(Thread.currentThread().getName());
            jkyclsVo.setCzfs("0");
            if(z.getJkxm().equals("y010107")){
                try {
                    jkyclsVo.setJkxm("正回购");
                    jkyclsVo.setJkzb("正回购实时监控");
                    jkyclsVo.setZy("customerzhg");
                    obj.setStatus(200);
                    ZhgVo zhgVo = new ZhgVo();
                    Map<String, Object> map = new HashMap<>();
                    String code = "";
                    for(int i=0;i<5;i++)
                    {
                        try {
                            jklxszDao.callprozhg(map);
                            code = String.valueOf(map.get("o_code"));
                            if (Integer.parseInt(code) == 1) {
                                zhgVo.setName("PRO_ZHG_SS");
                                zhgVo.setResult(Integer.parseInt(code));
                                jklxszDao.insertExecjjrRecord(zhgVo);
                                break;
                            }
                        } catch (Exception e) {
                            obj.setStatus(400);
                            throw new  MyException("正回购重算失败",e);
                        }
                    }
                    jkyclsVo.setZjls(code);
                    jkyclsVo.setXzjls(code);
                } catch (Exception e) {
                    jkyclsVo.setYcxx("错误信息:"+e.getMessage());
                    throw new  MyException("正回购重算失败",e);
                }finally {
                    jkyclsVo.setJssj(CommonUtil.getDateTime());
                    //异常监控流水记录
                    jklxszDao.insertzmycls(jkyclsVo);
                }
            }else{
                //TODO 经纪人监控
               /* jkyclsVo.setJkxm("经纪人");
                jkyclsVo.setJkzb("经纪人实时监控");
                jkyclsVo.setZy("customerzhg");
                obj.setStatus(200);
                //经纪人监控重算
                try {
                    ZhgVo zhgVo = new ZhgVo();
                    Map<String, Object> map = new HashMap<>();
                    for(int i=0;i<5;i++)
                    {
                        try {
                            jklxszDao.callprojjr(map);
                            if (Integer.parseInt(String.valueOf(map.get("o_code")))==1) {
                                zhgVo.setName("Z_JJR");
                                zhgVo.setResult(Integer.parseInt(String.valueOf(map.get("o_code"))));
                                jklxszDao.insertExecjjrRecord(zhgVo);
                                break;
                            }
                        } catch (Exception e) {
                            obj.setStatus(400);
                            throw new  MyException("经纪人重算失败",e);
                        }
                    }
                    jkyclsVo.setZjls(map.get("o_code").toString());
                    jkyclsVo.setXzjls(map.get("o_code").toString());
                } catch (Exception e) {
                    jkyclsVo.setYcxx("错误信息:"+e.getMessage());
                    throw new  MyException("经纪人重算失败",e);
                }finally {
                    jkyclsVo.setJssj(CommonUtil.getDateTime());
                    //异常监控流水记录
                    jklxszDao.insertzmycls(jkyclsVo);
                }*/
            }
        return obj;
    }

    @Override
    public ResponseObject trialdataurl(ZRuleVo z) {
        return new ResponseObject();
    }

    @Override
    public ResponseObject selectFZDictionary(ZRuleVo z) {
        ResponseObject obj = new ResponseObject();
        try{
            List<ZRuleVo> lsit = jklxszDao.selectFZDictionary(z);
            obj.setData(lsit);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("查询阀值字典表出错", e);
            throw new MyException("查询阀值字典表出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject selectzruleinfoById(ZRuleVo z) {
        ResponseObject obj = new ResponseObject();
        try{
            ZRuleVo ruleVo = jklxszDao.selectzruleinfoById(z);
            obj.setData(ruleVo);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("通过id查询账户实名制异常监控出错", e);
            throw new MyException("通过id查询账户实名制异常监控出错",e);
        }
        return obj;
    }

    @Transactional("viewTransactionManager")
    @Override
    public ResponseObject setZruleinfo(ZRuleVo z) {
        ResponseObject obj = new ResponseObject();
        try{
            if(!CommonUtil.isNotEmptyObj(z.getRemark())){
                z.setRemark("");
            }
            z.setSet_people(String.valueOf(z.getUserId()));
            z.setState(1);
            YcjkyzVo ycjkyz = new YcjkyzVo();
            if(!CommonUtil.isNotEmptyObj(z.getUid()))
            {
                jklxszDao.insertZruleinfo(z);
                ycjkyz.setName(z.getAbnormal_name());
                List<Map<String, Object>> list =  jklxszDao.selectZThresholdSet(ycjkyz);
                if(CommonUtil.isNotEmptyObj(list))
                {
                	String jsVal = String.valueOf(list.get(0).get("JSVAL"));
                    if( CommonUtil.isNotEmptyObj(jsVal))
                    {
                        String []jsval = jsVal.split(",");
                        for(String val : jsval)
                        {
                            ycjkyz.setId(z.getId());
                            ycjkyz.setName(val);
                            ycjkyz.setLowerlimit("无");
                            ycjkyz.setToplimit("无");
                            ycjkyz.setType(1);
                            ycjkyz.setState(1);
                            ycjkyz.setSet_people(String.valueOf(z.getUserId()));
                            jklxszDao.addZthreshold(ycjkyz);
                        }
                    }
                    String yjVal = String.valueOf(list.get(0).get("YJVAL"));
                    if(CommonUtil.isNotEmptyObj(yjVal))
                    {
                        String []yjval = yjVal.split(",");
                        for(String val : yjval)
                        {
                            ycjkyz.setId(z.getId());
                            ycjkyz.setName(val);
                            ycjkyz.setLowerlimit("无");
                            ycjkyz.setToplimit("无");
                            ycjkyz.setType(2);
                            ycjkyz.setState(1);
                            ycjkyz.setSet_people(String.valueOf(z.getUserId()));
                            jklxszDao.addZthreshold(ycjkyz);
                        }
                    }
                }
            }else{
                String abnormalname = jklxszDao.selectZRuleAbnormalnameById(z);

                jklxszDao.updateZruleinfo(z);

                if(!abnormalname.equals(z.getAbnormal_name()))
                {
                    ycjkyz.setRule_id(String.valueOf(z.getUid()));
                    ycjkyz.setState(2);
                    ycjkyz.setSet_people(String.valueOf(z.getUserId()));
                    jklxszDao.delZthreshold(ycjkyz);

                    ycjkyz.setName(z.getAbnormal_name());
                    List<Map<String, Object>> list =  jklxszDao.selectZThresholdSet(ycjkyz);
                    if(CommonUtil.isNotEmptyList(list))
                    {
                    	String jsVal = String.valueOf(list.get(0).get("JSVAL"));
                        if(CommonUtil.isNotEmptyObj(jsVal))
                        {
                            String []jsval = jsVal.split(",");
                            for(String val : jsval)
                            {
                                ycjkyz.setId(z.getId());
                                ycjkyz.setName(val);
                                ycjkyz.setLowerlimit("无");
                                ycjkyz.setToplimit("无");
                                ycjkyz.setType(1);
                                ycjkyz.setState(1);
                                ycjkyz.setSet_people(String.valueOf(z.getUserId()));
                                jklxszDao.addZthreshold(ycjkyz);
                            }
                        }
                        String yjVal = String.valueOf(list.get(0).get("YJVAL"));
                        if(CommonUtil.isNotEmptyObj(yjVal))
                        {
                            String []yjval = yjVal.split(",");
                            for(String val : yjval)
                            {
                                ycjkyz.setId(z.getId());
                                ycjkyz.setName(val);
                                ycjkyz.setLowerlimit("无");
                                ycjkyz.setToplimit("无");
                                ycjkyz.setType(2);
                                ycjkyz.setState(1);
                                ycjkyz.setSet_people(String.valueOf(z.getUserId()));
                                jklxszDao.addZthreshold(ycjkyz);
                            }
                        }
                    }
                }
            }
            obj.setStatus(200);
        }catch(Exception e){
            log.error("修改或新增监控信息错误", e);
            throw new MyException("修改或新增监控信息错误",e);
        }
        return obj;
    }

    @Override
    public ResponseObject queryHaveAbnormalid(ZRuleVo z) {
        ResponseObject obj = new ResponseObject();
        try{
            int a = jklxszDao.queryHaveAbnormalid(z);
            obj.setData(a);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("查询异常编号是否存在出错", e);
            throw new MyException("查询异常编号是否存在出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo selectzthresholdinfo(FzszVo f) {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            List<FzszVo> list =  jklxszDao.selectzthresholdinfo(f);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(list.size());
            obj.setCode(0);
        }catch(Exception e){
            log.error("查询异常监控阀值出错", e);
            throw new MyException("查询异常监控阀值出错",e);
        }
        return obj;
    }

    @Override
    public ResponseObject setZthreshold(FzszVo f) {
        ResponseObject obj = new ResponseObject();
        try{
            jklxszDao.updateZthreshold(f);
            obj.setStatus(200);
        }catch(Exception e){
            log.error("设置异常监控阀值出错", e);
            throw new MyException("设置异常监控阀值出错",e);
        }
        return obj;
    }

    @Override
    public LayuiTableVo anomalywater(JkyclsVo j) {
        LayuiTableVo obj = new LayuiTableVo();
        try{
            PageMethod.startPage(j.getPage(),j.getLimit());
            List<JkyclsVo> list =  jklxszDao.anomalywater(j);
            PageInfo<JkyclsVo> pageInfo = new PageInfo<>(list);
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch(Exception e){
            log.error("查询异常流水出错", e);
            throw new MyException("查询异常流水出错",e);
        }
        return obj;
    }
}
